/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.risk.setting.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.risk.setting.entity.RiskConfigScEntity;
import com.hopes.bizz.risk.setting.mapper.RiskConfigScMapper;
import com.hopes.bizz.risk.setting.service.RiskConfigScService;
import com.hopes.boss.common.core.exception.BizException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 风险后果设置
 *
 * @author pig
 * @date 2023-07-17 19:41:32
 */
@Service
public class RiskConfigScServiceImpl extends ServiceImpl<RiskConfigScMapper, RiskConfigScEntity> implements RiskConfigScService {

    @Override
    public boolean save(RiskConfigScEntity riskConfigSc) {
        if (null == riskConfigSc || StrUtil.isEmpty(riskConfigSc.getModuleCode()) || null == riskConfigSc.getLevelValue()
                || StrUtil.isEmpty(riskConfigSc.getCasualtyDesc()) || StrUtil.isEmpty(riskConfigSc.getPropertyLoss())
                || StrUtil.isEmpty(riskConfigSc.getEnvDesc()) || StrUtil.isEmpty(riskConfigSc.getFeimDesc()) || StrUtil.isEmpty(riskConfigSc.getLawDesc())) {
            throw new BizException("参数错误！");
        }
        // 根据等级查询是否存在
        final LambdaQueryWrapper<RiskConfigScEntity> queryWrapper = Wrappers.<RiskConfigScEntity>lambdaQuery().eq(RiskConfigScEntity::getLevelValue, riskConfigSc.getLevelValue());
        final List<RiskConfigScEntity> riskConfigScEntityList = baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(riskConfigScEntityList)) {
            throw new BizException("等级已存在！");
        }
        return baseMapper.insert(riskConfigSc) > 0;
    }

    @Override
    public boolean updateById(RiskConfigScEntity riskConfigSc) {
        if (null == riskConfigSc || null == riskConfigSc.getId() || StrUtil.isEmpty(riskConfigSc.getModuleCode())
                || null == riskConfigSc.getLevelValue() || StrUtil.isEmpty(riskConfigSc.getCasualtyDesc()) || StrUtil.isEmpty(riskConfigSc.getPropertyLoss())
                || StrUtil.isEmpty(riskConfigSc.getEnvDesc()) || StrUtil.isEmpty(riskConfigSc.getFeimDesc()) || StrUtil.isEmpty(riskConfigSc.getLawDesc())) {
            throw new BizException("参数错误！");
        }
        // 根据等级查询是否存在
        final LambdaQueryWrapper<RiskConfigScEntity> queryWrapper = Wrappers.<RiskConfigScEntity>lambdaQuery()
                .eq(RiskConfigScEntity::getLevelValue, riskConfigSc.getLevelValue()).ne(RiskConfigScEntity::getId, riskConfigSc.getId());
        final List<RiskConfigScEntity> riskConfigScEntityList = baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(riskConfigScEntityList)) {
            throw new BizException("等级已存在");
        }
        return baseMapper.updateById(riskConfigSc) > 0;
    }

}
